package com.sun.umbrella.manager.res;

import com.sun.core.enums.BaseBizEnum;
import com.sun.core.enums.BaseErrorEnum;
import com.sun.core.ex.BusinessProcessFailException;
import com.sun.umbrella.dao.ResInfoMapper;
import com.sun.umbrella.domain.bo.ResInfoBO;
import com.sun.umbrella.domain.entity.ResInfoDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * @description 资源基础数据manager实现类
 * @author: Jay
 * @date: 2018-04-09 16:30
 * @version: v1.0
 */
@Component
@Slf4j
public class ResInfoManager {


    @Autowired
    private ResInfoMapper resInfoMapper;

    /**
     * 资源信息保存
     *
     * @param offset
     * @param resInfoDO
     */
    @Transactional(rollbackFor = BusinessProcessFailException.class)
    public Boolean saveRes(ResInfoDO resInfoDO, Integer offset) {
        // 将所有的在该节点右边的节点 左右值 全部 +2
        resInfoMapper.updateLeftNodeAdd2(offset, resInfoDO.getSysId());
        resInfoMapper.updateRightNodeAdd2(offset, resInfoDO.getSysId());
        // 插入节点
        Integer count = resInfoMapper.insertSelective(resInfoDO);
        if (count != 1) {
            log.error(this.getClass().getName() + "#saveRes,prarms is {}", resInfoDO);
            throw new BusinessProcessFailException("资源信息保存失败", BaseErrorEnum.SAVE_ERROR.getCode());
        }
        return Boolean.TRUE;
    }


    /**
     * 删除资源节点
     *
     * @param sysId
     * @param index
     * @param id
     * @return
     */
    @Transactional(rollbackFor = BusinessProcessFailException.class)
    public Boolean deleteRes(Integer sysId, Integer index, Integer id) {
        Integer countDelete = resInfoMapper.updateIsDeletedById(id, sysId);
        if (countDelete == null || !countDelete.equals(BaseBizEnum.FIRST.getKey())) {
            log.error(this.getClass().getName() + "#deleteRes,prarms is {}", id);
            throw new BusinessProcessFailException("资源信息删除失败", BaseErrorEnum.UPDATE_ERROR.getCode());
        }
        // 被删除的节点的右边的节点的左右值都要 -2
        resInfoMapper.updateLeftNodeWhenDel(index, sysId);
        resInfoMapper.updateRgtNodeWhenDel(index, sysId);
        return Boolean.TRUE;
    }


    /**
     * 更新资源信息
     *
     * @param resInfoDO
     */
    @Transactional(rollbackFor = BusinessProcessFailException.class)
    public Boolean updateResWithoutMoveNode(ResInfoDO resInfoDO) {
        Integer count = resInfoMapper.updateResInfoSelective(resInfoDO);
        if (count == null || !count.equals(BaseBizEnum.FIRST.getKey())) {
            log.error(this.getClass().getName() + "#updateResWithoutMoveNode,param is {}", resInfoDO);
            throw new BusinessProcessFailException("更新资源失败", BaseErrorEnum.UPDATE_ERROR.getCode());
        }
        return Boolean.TRUE;
    }


    /**
     * 根据sysId和id更新资源左右值
     *
     * @param resInfoBO
     */
    @Transactional(rollbackFor = BusinessProcessFailException.class)
    public Boolean updateResLftAndRgt(ResInfoBO resInfoBO) {
        Integer count = resInfoMapper.updateResLftAndRgt(resInfoBO);
        if (count == null || !count.equals(BaseBizEnum.FIRST.getKey())) {
            log.error(this.getClass().getName() + "#updateResLftAndRgt,param is {}", resInfoBO);
            throw new BusinessProcessFailException("更新资源失败", BaseErrorEnum.UPDATE_ERROR.getCode());
        }
        return Boolean.TRUE;
    }


}
